import { setupHttpConfig } from '../config/http-config';

// 使用@tuxing/network-http组件创建的请求实例
const request = setupHttpConfig();

// 为验证码图片请求添加特殊处理
// 注意：@tuxing/network-http组件已经处理了大部分逻辑，这里只处理验证码图片的特殊需求
const handleCaptchaRequest = async (config: any) => {
  // 使用request实例处理验证码请求
  return request.get(config);
};

// Login API interfaces
export interface LoginParam {
  username: string;
  password: string;
  code?: string;
  uuid?: string;
  grantType?: string;
  clientId?: string;
}

// 🆕 手机号登录参数
export interface PhoneLoginParam {
  phone: string;
  smsCode: string;
  grantType?: string;
  clientId?: string;
}

// 🆕 发送短信参数
export interface SendSmsParam {
  phone: string;
  type: 'login' | 'register' | 'forgot_password' | 'reset_password'; // 短信类型
}

// 🆕 忘记密码参数
export interface ForgotPasswordParam {
  phone: string;
  smsCode: string;
  newPassword: string;
}

export interface RegisterBody {
  username: string;
  password: string;
  phone?: string;
  smsCode?: string;
  code?: string;
  uuid?: string;
  userType: string;
}

export interface TenantListVo {
  tenantId: string;
  companyName: string;
}

export interface CaptchaResponse {
  captchaEnabled: boolean;
  img: string;
  uuid: string;
}

export interface LoginResponse {
  access_token: string;
  refresh_token: string;
  user_info: {
    username: string;
    nickname: string;
    avatar: string;
    roles: string[];
    permissions: string[];
  };
}
// API functions
// 定义请求返回的数据结构
export interface LoginApiResponse {
  data: LoginResponse;
}

export const login = (data: LoginParam) => {
  return request.post<LoginApiResponse>({
    url: '/auth/login',
    data,
  });
};

export const register = (data: RegisterBody) => {
  return request.post({
    url: '/auth/register',
    data,
  });
};

export const getCodeImg = async () => {
  // 特殊处理验证码图片请求
  try {
    // 获取请求配置
    const config = {
      url: '/auth/code',
      timeout: 10000,
    };
    // 使用专门的处理函数
    return await handleCaptchaRequest(config);
  } catch (error) {
    console.error('获取验证码失败:', error);
    throw error;
  }
};

export const getTenantList = (voList = true) => {
  return request.get<{ tenantEnabled: boolean; voList: TenantListVo[] }>({
    url: `/auth/tenant/list?voList=${voList}`,
  });
};

export const authBinding = (type: string) => {
  return request.get<string>({
    url: `/auth/social/binding/${type}`,
  });
};

// 🆕 发送短信验证码 API
export const sendSmsCode = (data: SendSmsParam) => {
  return request.post({
    url: '/auth/sms/send',
    data,
  });
};

// 🆕 校验短信验证码 API
export const verifySmsCode = (phone: string, code: string, type: string) => {
  return request.post({
    url: '/auth/sms/verify',
    data: { phone, code, type },
  });
};

// 🆕 手机号登录 API
export const phoneLogin = (data: PhoneLoginParam) => {
  return request.post<LoginApiResponse>({
    url: '/auth/login/phone',
    data,
  });
};

// 🆕 忘记密码 - 验证手机号 API
export const forgotPasswordVerify = (phone: string) => {
  return request.post({
    url: '/auth/forgot-password/verify',
    data: { phone },
  });
};

// 🆕 重置密码 API
export const resetPassword = (data: ForgotPasswordParam) => {
  return request.post({
    url: '/auth/reset-password',
    data,
  });
};

export default request;
